package nt.easyexcel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
//import com.cebbank.essp.decisionconfigurecenter.ruleset.model.vo.RuleSetVersionInAndOutImportModel;
//import com.cebbank.poin.core.log.Logger;
//import com.cebbank.poin.core.log.LoggerFactory;
import org.assertj.core.util.Strings;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author w45865
 * @version 1.0
 * @title InAndOutExcelListener
 * @description
 * @create 2023/10/18 10:01
 * @Modified by
 * @Modified 2023/10/18 10:01
 * @Modified description
 */
@Component
public class InAndOutExcelListener extends AnalysisEventListener<RuleSetVersionInAndOutImportModel> {

    //存储数据
    private List<RuleSetVersionInAndOutImportModel> list = new ArrayList<>();
    //记录条数
    private List<Integer> sumList = new ArrayList<>();


    //校验是否符合模板
    @Override
    public void invokeHeadMap(Map<Integer,String> headMap, AnalysisContext context) {
        Field[] fields = RuleSetVersionInAndOutImportModel.class.getDeclaredFields();
        for (Field field : fields) {
            //获取当前字段上的ExcelProperty注解信息
            ExcelProperty fieldAnnotation = field.getAnnotation(ExcelProperty.class);
            //判断当前字段上是否存在ExcelProperty的注解
            if (fieldAnnotation != null) {
                //存在ExcelProperty注解则根据ExcelProperty注解的index索引到表头中获取对应的表头名
                String headName = headMap.get(fieldAnnotation.index());
                if (Strings.isNullOrEmpty(headName) || !headName.equals(fieldAnnotation.value()[0])) {
                    throw new RuntimeException("模板错误，请导入正确的模板!");
                }
            }
        }
    }

    @Override
    public void invoke(RuleSetVersionInAndOutImportModel model, AnalysisContext analysisContext) {
        list.add(model);
    }
    //收尾
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        Integer sum = list.size();  //拿到此时的list长度
        sumList.add(sum);
        doSomething();
    }

    //待补充，暂时叫个这个方法名，先提交让前端可以联调
    public void doSomething() {
        // todo
    }

    public List<RuleSetVersionInAndOutImportModel> getList() {
        return list;
    }

    public void setList(List<RuleSetVersionInAndOutImportModel> list) {
        this.list = list;
    }

    public List<Integer> getSumList() {
        return sumList;
    }

    public void setSumList(List<Integer> sumList) {
        this.sumList = sumList;
    }
}
